const userRouter = require('express').Router()
const userController = require('../constrollers/user')
const userSchema = require('../schemas/user')
const expressJoi = require('@escook/express-joi')
const multer = require('multer')
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/images/')
  },
  filename: function (req, file, cb) {
    var fileFormat = (file.originalname).split(".");
    cb(null, req.user.username+ "." + fileFormat[fileFormat.length - 1]);
  }
})
const storage2 = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/images/')
  },
  filename: function (req, file, cb) {
    var fileFormat = (file.originalname).split(".");
    cb(null, 'bg_'+req.user.username+ "." + fileFormat[fileFormat.length - 1]);
  }
})
const upload = multer({ storage: storage })
const upload2 = multer({ storage: storage2 })

// 验证码
userRouter.post('/api/code', expressJoi(userSchema.sendCode), userController.sendCode)
// 登录
userRouter.post('/api/login', expressJoi(userSchema.login), userController.login)
// 用户信息
userRouter.get('/userinfo', userController.getUserinfo)
// 上传头像
userRouter.post('/update/avatar',upload.single('avatarFile'), userController.upadateAvatar)
// 修改昵称
userRouter.post('/update/nickname',expressJoi(userSchema.updateNickname), userController.updateNickname)
// 修改性别
userRouter.post('/update/gender',expressJoi(userSchema.updateGender), userController.updateGender)
// 修改生日
userRouter.post('/update/birthday',expressJoi(userSchema.updateBirthday), userController.updateBirthday)
// 修改所在地
userRouter.post('/update/address',expressJoi(userSchema.updateAddress), userController.updateAddress)
// 修改学校
userRouter.post('/update/school',expressJoi(userSchema.updateSchool), userController.updateSchool)
// 修改个人介绍
userRouter.post('/update/signature',expressJoi(userSchema.updateSignature), userController.updateSignature)
// 上传背景
userRouter.post('/update/background',upload2.single('backgroundFile'), userController.updateBackground)
// 身份验证
userRouter.post('/verify',expressJoi(userSchema.verifyID), userController.verifyID)
// 设置密码
userRouter.post('/update/password',expressJoi(userSchema.updatePwd), userController.updatePassword)
// 更换手机号
userRouter.post('/update/phone',expressJoi(userSchema.updatePhone), userController.updatePhone)


module.exports = userRouter